package com.egg.auth.service.job.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.auth.entity.job.JobLinkUser;
import com.egg.auth.mapper.job.JobLinkUserMapper;
import com.egg.auth.service.job.IJobLinkUserService;
import com.egg.common.mybatis.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
public class JobLinkUserServiceImpl extends BaseServiceImpl<JobLinkUserMapper, JobLinkUser> implements IJobLinkUserService {


    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void put(Serializable jobId, Collection<? extends Serializable> userIds) {
        if (ObjectUtil.isEmpty(jobId)) {
            return;
        }
        delByJobId(jobId);
        if (ObjectUtil.isEmpty(userIds)) {
            return;
        }
        add(jobId, userIds);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(Serializable jobId, Collection<? extends Serializable> userIds) {
        if (ObjectUtil.isEmpty(jobId)) {
            return;
        }
        List<JobLinkUser> jobLinkUserList = findByJobIdAndUserId(jobId, userIds);
        Collection<? extends Serializable> existUserIds = jobLinkUserList.stream().map(o -> o.getUserId()).collect(Collectors.toSet());
        Collection<? extends Serializable> addUserIds = userIds.stream().filter(o -> !existUserIds.contains(o)).collect(Collectors.toSet());
        List<JobLinkUser> list = new ArrayList<>();
        addUserIds.forEach(o -> {
            JobLinkUser deptUser = new JobLinkUser();
            deptUser.setJobId(Long.parseLong(jobId.toString()));
            deptUser.setUserId(Long.parseLong(o.toString()));
            list.add(deptUser);
        });
        baseSave(list);
    }

    @Override
    public List<JobLinkUser> findByJobIdAndUserId(Serializable jobId, Collection<? extends Serializable> userId) {
        if (ObjectUtil.isEmpty(jobId) || ObjectUtil.isEmpty(userId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<JobLinkUser> qw = buildLambdaSortQw();
        qw.eq(JobLinkUser::getJobId, jobId)
                .in(JobLinkUser::getUserId, userId);
        return list(qw);
    }

    @Override
    public List<JobLinkUser> findByJobId(Collection<? extends Serializable> jobId) {
        if (ObjectUtil.isEmpty(jobId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<JobLinkUser> qw = buildLambdaSortQw();
        qw.in(JobLinkUser::getJobId, jobId);
        return list(qw);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delByJobId(Serializable jobId) {
        if (ObjectUtil.isEmpty(jobId)) {
            return;
        }
        delByJobId(Arrays.asList(jobId));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delByJobId(Collection<? extends Serializable> jobId) {
        if (ObjectUtil.isEmpty(jobId)) {
            return;
        }
        LambdaQueryWrapper<JobLinkUser> qw = buildLambdaQw();
        qw.in(JobLinkUser::getJobId, jobId);
        remove(qw);
    }

    @Override
    public Page<JobLinkUser> page(
            Integer current, Integer pageSize
            , Optional<Serializable> jobId
    ) {
        LambdaQueryWrapper<JobLinkUser> qw = buildLambdaSortQw();
        jobId.ifPresent(o -> qw.eq(JobLinkUser::getJobId, o));
        return page(new Page<>(current, pageSize), qw);
    }


}
